package com.cloud.drore.eboss.operation.service.impl;

import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.base.ResultMessage;
import com.cloud.drore.eboos.common.constant.CommonConstant;
import com.cloud.drore.eboos.common.constant.ResultCodeConstant;
import com.cloud.drore.eboos.common.entity.supplier.ProductRelation;
import com.cloud.drore.eboos.exception.BusinessException;
import com.cloud.drore.eboss.operation.entity.po.OTAGetProductRequest;
import com.cloud.drore.eboss.operation.entity.vo.OTAGetProductResponse;
import com.cloud.drore.eboss.operation.mapper.FactoryGetProductMapper;
import com.cloud.drore.eboss.operation.supplierApiService.IFactoryGetProductApiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/3/9 17:59 <br>
 * @Author: <a href="1451173193@qq.com">杨亮</a>
 */
@Service
public class FactoryGetProductApiServiceImpl implements IFactoryGetProductApiService {
    @Autowired
    private FactoryGetProductMapper factoryGetProductMapper;

    /**
     * @param pagePo
     * @ClassName: FactoryGetProductApiServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage
     * @Decription:OTA获取产品信息
     * @CreateDate: Created in 2018/3/9 18:00
     * @Author: <a href="1451173193@qq.com">杨亮</a>
     * @Modify:
     */
    @Override
    public ResultMessage getProductInfo(PagePo<OTAGetProductRequest> pagePo) throws BusinessException {
        OTAGetProductRequest reqpo = pagePo.getFields();
        //必填字段验证，目前框架不支持分页字段注解验证
        if (reqpo.getDistributorId() == null) {
            throw new BusinessException(ResultCodeConstant.SERVICE_BUSSNISS_CODE, "分销商id不能为空");
        }
        if (reqpo.getMethod() == null || reqpo.getMethod().equals("")) {
            throw new BusinessException(ResultCodeConstant.SERVICE_BUSSNISS_CODE, "获取数据方式method不能为空");
        }
        if (!CommonConstant.TYPE_OF_TICKET.equals(reqpo.getProductType()) && !CommonConstant.TYPE_OF_COMBINATION.equals(reqpo.getProductType())) {
            throw new BusinessException(ResultCodeConstant.SERVICE_BUSSNISS_CODE, "产品类型有误");
        }
        List<OTAGetProductResponse> list=new ArrayList<>();//返回list
        if (CommonConstant.METHOD_SINGLE.equals(reqpo.getMethod())) {//单个产品
            if (reqpo.getProductNum() == null || reqpo.getProductNum().equals("")) {//编号为空
                throw new BusinessException(ResultCodeConstant.SERVICE_BUSSNISS_CODE, "产品编号不能为空");
            }
            String isOk = factoryGetProductMapper.validateDistributor(reqpo.getDistributorId());
            if (isOk == null || isOk.equals("NO")) {//验证分销商
                throw new BusinessException(ResultCodeConstant.SERVICE_BUSSNISS_CODE, CommonConstant.VALIDATE_ERROR_OF_DIS);
            }
            OTAGetProductResponse resVo = factoryGetProductMapper.getProductInfoSingle(reqpo);
            if (resVo == null) {
                throw new BusinessException(ResultCodeConstant.SERVICE_BUSSNISS_CODE, "产品编号错误或者该产品已下架");
            }
            list.add(resVo);
            PageInfo pageInfo = new PageInfo<>(list);
            return ResultMessage.success(pageInfo);

        } else if (CommonConstant.METHOD_ALL.equals(reqpo.getMethod())) {//全部产品
            String isOk = factoryGetProductMapper.validateDistributor(reqpo.getDistributorId());
            if (isOk == null || isOk.equals("NO")) {//验证分销商
                throw new BusinessException(ResultCodeConstant.SERVICE_BUSSNISS_CODE, CommonConstant.VALIDATE_ERROR_OF_DIS);
            }
            if (pagePo.getPageNum() == null || pagePo.getPageNum().equals("") || pagePo.getPageSize() == null || pagePo.getPageSize().equals("")) {
                throw new BusinessException(ResultCodeConstant.SERVICE_BUSSNISS_CODE, "分页参数不能为空");
            }
            PageHelper.startPage(pagePo);
            list = factoryGetProductMapper.getProductInfoAll(pagePo.getFields());
            PageInfo pageInfo = new PageInfo<>(list);
            return ResultMessage.success(pageInfo);
        } else {//不支持的method
            throw new BusinessException(ResultCodeConstant.SERVICE_BUSSNISS_CODE, "method参数错误");
        }
    }

    /**
     * @param product
     * @ClassName: FactoryGetProductApiServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage<com.cloud.drore.eboos.common.entity.supplier.ProductRelation>
     * @Decription:旅行社与ota对款接口专用
     * @CreateDate: Created in 2018/3/20 15:43
     * @Author: <a href="1451173193@qq.com">杨亮</a>
     * @Modify:
     */
    @Override
    public ResultMessage<ProductRelation> getProductIdByNum(ProductRelation product) throws BusinessException {
        if ("".equals(product.getProductNum())) {
            throw new BusinessException("产品编号不为空！");
        }
        return new ResultMessage(factoryGetProductMapper.getProductIdByNum(product));
    }
}
